package com.woniu.cloud.movie.movie3assembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.woniu.cloud.movie.api.dto.MovieDto;
import com.woniu.cloud.movie.api.dto.MoviesDto;
import com.woniu.cloud.movie.api.exception.mv.MovieException;
import com.woniu.cloud.movie.api.param.movieParam.MovieAddParam;
import com.woniu.cloud.movie.api.param.movieParam.MovieChangeParam;
import com.woniu.cloud.movie.api.param.movieParam.MovieDeleteParam;
import com.woniu.cloud.movie.api.param.movieParam.MovieLikeGetsParam;
import com.woniu.cloud.movie.movie3assembly.entity.Movie;
import com.woniu.cloud.movie.movie3assembly.mapper.MovieMapper;
import com.woniu.cloud.movie.movie3assembly.service.MovieService;
import com.woniu.cloud.movie.movie3assembly.util.ComUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2022-01-11
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {
    @Override
    public MovieDto saveMovie(MovieAddParam movieAddParam) throws MovieException {
        Movie movie = ComUtil.copyProperties(movieAddParam, Movie.class);
        movie.setStatus(0);
        movie.setIsDelete(0);
        movie.setUpstatus(0);
        boolean save = save(movie);
        if (save)
            return ComUtil.copyProperties(movie,MovieDto.class);
        throw new MovieException("添加失败");
    }

    @Override
    public MovieDto upMovie(Integer id) throws MovieException {
        Movie byId = getById(id);

        if(byId==null){
            throw new MovieException("未找到该电影");
        }
        if (byId.getIsDelete()==1){
            throw new MovieException("该电影已过期");
        }
        if (byId.getStatus()==1){
            throw new MovieException("该电影已经上架");
        }
        if (byId.getStatus()==0){
            byId.setStatus(1);
            boolean b = updateById(byId);
            if (b)
                return ComUtil.copyProperties(byId,MovieDto.class);
            throw new MovieException("上架失败");
        }
        throw new MovieException("该电影不能上架");

    }

    @Override
    public MovieDto downMovie(Integer id) throws MovieException {
        Movie byId = getById(id);
        if(byId==null){
            throw new MovieException("未找到该电影");
        }
        if (byId.getStatus()==0){
            throw new MovieException("该电影已经处于下架状态");
        }
        if (byId.getIsDelete()==1){
            throw new MovieException("该电影已过期");
        }
        byId.setStatus(0);
        boolean b = updateById(byId);
        if (b)
            return ComUtil.copyProperties(byId,MovieDto.class);
        throw new MovieException("下架失败");
    }

    @Override
    public MoviesDto likeMovie(MovieLikeGetsParam movieLikeGetsParam) {
        QueryWrapper<Movie> qw = new QueryWrapper<>();
        qw.eq("isDelete",0);
        if (movieLikeGetsParam.getLikeName()!=null){
            qw.and(qr ->{
                qr.like("movie_name",movieLikeGetsParam.getLikeName())
                        .or()
                        .like("message",movieLikeGetsParam.getLikeName());
            });
        }
        if (movieLikeGetsParam.getStatu()!=null){
            qw.eq("status",movieLikeGetsParam.getStatu());
        }
        qw.orderByDesc("movie_id");
        Page<Movie> p = new Page<>();
        p.setCurrent(movieLikeGetsParam.getPageIndex());
        p.setSize(movieLikeGetsParam.getPageSize());
        Page<Movie> page1 = page(p, qw);

        MoviesDto moviesDto = new MoviesDto();
        moviesDto.setMoviesDtos(ComUtil.copyList(page1.getRecords(),MovieDto.class));
        moviesDto.setPageIndex(page1.getCurrent());
        moviesDto.setTotal(page1.getTotal());
        return moviesDto;
    }

    @Override
    public boolean deleteMovie(Integer id) throws MovieException {
        Movie byId = getById(id);
        if(byId==null){
            throw new MovieException("未找到该电影");
        }
        if (byId.getUpstatus()==1){
            throw new MovieException("该电影不可删除");
        }
        if (byId.getIsDelete()==1){
            throw new MovieException("该电影已删除");
        }
        byId.setIsDelete(1);
        boolean b = updateById(byId);
        return b;
    }

    @Override
    public boolean deleteMovies(MovieDeleteParam movieDeleteParam) throws MovieException {
        List<Integer> ids = movieDeleteParam.getIds();

        QueryWrapper<Movie> qw = new QueryWrapper<>();
        qw.in("movie_id",ids);
        qw.eq("isDelete",0);
        List<Movie> movies = list(qw);
        if (ids.size()!=movies.size())
            throw new MovieException("存在电影未被找到");
        for (Movie movie : movies) {
            movie.setIsDelete(1);
            if (movie.getUpstatus()==1)
                throw new MovieException("存在电影不可被删除");
        }

        boolean b = updateBatchById(movies);
        return b;
    }

    @Override
    public boolean updateMovie(MovieChangeParam movieChangeParam) throws MovieException {
        Movie byId = getById(movieChangeParam.getMovieId());
        if (byId==null||byId.getIsDelete()==1){
            throw new MovieException("要修改的电影不存在");
        }
        if (byId.getUpstatus()==0){
            BeanUtils.copyProperties(movieChangeParam,byId);
            boolean b = updateById(byId);
            if (b)
                return true;
            throw new MovieException("修改失败");
        }
        throw new MovieException("该电影无法修改");

    }
}
